package kw.android.AProject;

import java.util.ArrayList;
import java.util.List;

import kw.android.AProject.Sprite.Sprite;
import kw.android.AProject.Sprite.SpriteButton;
import kw.android.AProject.Sprite.SpriteImageView;
import kw.android.AProject.Sprite.SpriteStreet;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Toast;

public class MapBackground extends SurfaceView implements Callback {
	
	private Context 					mContext;
	private SurfaceHolder 				mHolder;
	public  MainThread 					mThread;
	private MultiTouch 					mMultiTouch;
	
	private List<Sprite> 				mSpriteContainer;
	private SpriteImageView 			mMapImage;
	private SpriteStreet 				mStreet;
	private SpriteButton 				mButton;
	
	public MapBackground(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context; 		
		init();
	}
	
	private void init() {
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		mHolder = holder; // 생성한 holder를 전역변수에 저장
		mThread = new MainThread(); 		
		mMultiTouch = new MultiTouch();
		mSpriteContainer = new ArrayList<Sprite>();
		
		mMapImage = new SpriteImageView(mContext);
		mMapImage.setImageRes(R.drawable.map);
		mMapImage.setSize(1500, 3000);
		
		mStreet = new SpriteStreet(mContext);
		mStreet.setImageRes(R.drawable.icon);				
		mStreet.setSize(30, 200);
		mStreet.setLocation(400, 300);
		
		mButton = new SpriteButton(mContext);
		mButton.setImageRes(R.drawable.icon);
		mButton.setSize(100, 100);
		mButton.setLocation(250, 100);
		
		mSpriteContainer.add(mMapImage);
		mSpriteContainer.add(mStreet);
		mSpriteContainer.add(mButton);
	}

	// -------------------------------------
	// SurfaceView가 만들어질 때 호출됨
	// -------------------------------------
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mThread.start();
	}

	// -------------------------------------
	// SurfaceView의 크기가 바뀔 때 호출됨
	// -------------------------------------
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	// -------------------------------------
	// SurfaceView가 종료될 때 호출됨
	// -------------------------------------
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean done = true;
		while (done) {
			try {
				mThread.join(); // 스레드가 현재 step을 끝낼 때 까지 대기
				mThread.destroy();
				done = false;
			} catch (InterruptedException e) { // 인터럽트가 발생하면?
				// 그 신호 무시 - 아무것도 않음
			}
		} // while
	}
	
	private boolean isRunning=true;
	class MainThread extends Thread {
		public MainThread() {
					
		}

		public void run() {
			Canvas canvas = null; 
			while (isRunning) {
				canvas = mHolder.lockCanvas(); // canvas를 잠그고 버퍼 할당
				try {
					synchronized (mHolder) {
						canvas.drawColor(Color.BLACK);
						
						// mMatrix 을 이ㅛ용해서 늘려진 사이즈 등을 알고 좌표를 구하자.
						
						mMapImage.doDrawWithMatrix(canvas, mMatrix);						
						mStreet.doDrawWithMatrix(canvas, mMatrix);						
						mButton.doDrawWithMatrix(canvas, mMatrix);
					}
				} finally {
					if (canvas != null)
						mHolder.unlockCanvasAndPost(canvas); // canvas의 내용을 View에 전송
				}
			}
		} // while
	} // End of Thread
					
	Matrix mMatrix = new Matrix();
	float[] mValues = new float[9];
	// touch
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int)event.getX(), y = (int)event.getY();

		mMatrix = mMultiTouch.doMultiTouch(event);		
		
		switch(event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			if(mStreet.isTouched(x, y)) {
				// 거리 클릭.
//				Toast.makeText(mContext, "street is touched", Toast.LENGTH_SHORT).show();
				mContext.startActivity(new Intent(mContext, StreetActivity.class));
			}
			if(mButton.isTouched(x, y)) {
				// 버튼 클릭.
//				Toast.makeText(mContext, "button is touched", Toast.LENGTH_SHORT).show();
				mContext.startActivity(new Intent(mContext, ShopInfoActivity.class));
			}
			break;
		case MotionEvent.ACTION_UP:
			mMatrix.getValues(mValues);
			
			mButton.setArea(mValues[2], mValues[5], mValues[0], mValues[0]);
			mStreet.setArea(mValues[2], mValues[5], mValues[0], mValues[0]);
			
			break;
		}
		return true;
	} // touch	
				
	public void setIsRunning(boolean set) {
		isRunning = set;
	}
}
